<?php

namespace App\Models\Factory;


use App\Constants\InviteConstant;
use App\Helpers\Logger\SLogger;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\AwUserInvite;
use App\Models\Orm\UserInvite;
use App\Models\Orm\UserInviteCode;
use App\Models\Orm\UserInviteLog;
use App\Strategies\InviteStrategy;


/**
 * 用户工厂
 * Class UserFactory
 * @package App\Models\Factory
 */
class UserInviteCodeFactory extends AbsModelFactory
{
    /**
     * 通过邀请码
     */
    public static function getUserCodeStatus($code)
    {
        $data['code'] = $code;
        $result = UserInviteCode::where($data)->first();
       return $result ? $result :'';

    }

    //查找用户邀请表,拿到用户邀请人数
    public static  function addUserInvite($userId){

        $data['update_ip'] = Utils::ipAddress();
        $userInvite = UserInvite::where('user_id',$userId)->first();


        if($userInvite){
            $data['invite_num'] = $userInvite ->invite_num +1;
            $re = UserInvite::where('user_id',$userId)->update($data);
            return $re ? true :false;
        }else{
            $data['invite_num'] = 1;
            $data['user_id'] = $userId;;
            $re = UserInvite::create($data);
            return $re ? true :false;

        }
    }

    //判断该用户是否已经被邀请注册
    public static  function getInviteUser($mobile)
    {
        $result = AwUserInvite::where('phone',$mobile)->whereIn('status', [AwUserInvite::STATUS_REGISTER,AwUserInvite::STATUS_CHECK])->first();
        return $result ? true : false;

    }


    //生成邀请码
    public static function fetchInviteCode($userId){


        //插入到邀请码中
        $inviteCode = UserInviteCode::firstOrCreate(['user_id' => $userId],[
            'user_id' => $userId,
            'code' => InviteStrategy::createCode(),
            'status' => 1,
            'created_ip'      => Utils::ipAddress(),
            'created_at'      => date('Y-m-d H:i:s', time()),
            'updated_at'      => date('Y-m-d H:i:s', time()),
        ]);

        

        return $inviteCode ->code ? $inviteCode ->code : '';

    }


    //更新邀请日志表

    public static function  updateUserInviteLog($userphone,$user_id)
    {
        //邀请好友个数
        $invite_num = AwUserInvite::where('user_id',$user_id)->count();

        switch ($invite_num)
        {
            case 0 :
                $invite['integral'] = AwUserInvite::INVITE_CODE_FIRST;
                break;
            case 1 :
                $invite['integral'] = AwUserInvite::INVITE_CODE_SECOND;
                break;
            case 2 :
                $invite['integral'] = AwUserInvite::INVITE_CODE_THIRD;
                break;
            default:
                $invite['integral'] = 0;
                break;

        }

        if($invite['integral'] == 0)
        {
            return false;
        }

            //通过手机号得到被邀请人idf
        $userId = UserFactory::getUserByMobile($userphone)->id;
        $data['invited_id']= $userId;
        $data['integral'] = $invite['integral'];
        $data['phone'] = $userphone;
        $data['status'] = AwUserInvite::STATUS_INIT;//已经注册
        //邀请人id
        $data['user_id'] = $user_id;
        $data['ip'] = strval(ip2long(Utils::ipAddress()));
        $data['created_at'] = date('Y-m-d H:i:s', time());
        $result = AwUserInvite::updateOrCreate(['phone'=>$userphone],$data);

        return $result ? $invite['integral'] : false;
    }

    //创建用户邀请日志
    public static function  createUserInviteLog($userId,$phone)
    {
        $data['mobile'] = $phone;
        $data['user_id']= $userId;
        $data['from']  = InviteConstant::INVITE_SMS;
        $data['code']  = UserInviteCodeFactory::fetchInviteCode($userId);
        $data['status']  = InviteConstant::INVITE_ING;
        $data['created_user_id']   = $userId;
        $data['created_at']   = date('Y-m-d H:i:s', time());
        $data['created_ip']   =Utils::ipAddress();
        $userInvite = UserInviteLog::firstOrCreate(['mobile' => $phone],$data);

       return  $userInvite ? true : false;
    }


    //查看用户是否是被邀请的用户
    public static  function  fetchUser($userphone)
    {
       $user = UserInviteLog::where('mobile',$userphone)->first();

        return $user ? true : false;
    }


    //修改用户邀请日志表状态
    public static  function updateInviteLog($userphone)
    {
        $data['status'] = InviteConstant::INVITE_REGISTER;
        //修改状态为已填写
        $result = UserInviteLog::where('mobile',$userphone)-> update($data);
        return  $result ? true : false;
    }


    //获取用户的邀请记录
    public static  function fetchUserInviteLog($userId)
    {
        $result = UserInviteLog::where('user_id',$userId)->select('mobile','status')->get()->toArray();

        foreach ($result as $k => $v )
        {

            switch ($result[$k]['status'])
            {
                case 1 :
                    $result[$k]['status'] = '邀请中';
                    $result[$k]['money'] = '￥0.00';
                    break;
                case 2 :
                    $result[$k]['status'] = '已注册';
                    $result[$k]['money'] = '￥0.00';
                    break;
                case 3 :
                    $result[$k]['status'] = '已填写';
                    $result[$k]['money'] = '￥15.00';
                    break;
            }

        }

        return $result ? $result :array();
    }


    //查看用户是否注册并且填写
    public static function getRegisterStatus($mobile)
    {
       $result = UserInviteLog::where('mobile',$mobile)->first();
        return $result? true : false;
    }
}
